package com.example.onlineeducation.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.db.datastore.UserInfoDataStoreKey
import com.example.onlineeducation.data.entity.BannerEntity
import com.example.onlineeducation.data.entity.UserProJoinInfoEntity
import com.example.onlineeducation.data.entity.UserProStudyChartEntity
import com.example.onlineeducation.data.entity.UserProStudyTimeEntity
import com.example.onlineeducation.data.json.UserInfoJson
import com.example.onlineeducation.data.json.UserProJoinInfoJson
import com.example.onlineeducation.repository.OfflineRepository
import com.example.onlineeducation.repository.StudyRepository
import com.example.onlineeducation.repository.UserRepository
import com.example.onlineeducation.utils.YcTimeUtils
import com.yc.jetpacklib.net.YcResult
import com.yc.jetpacklib.utils.YcTime
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.zip

class MainMineVM : BaseViewModel() {

    private val mStudyRepository: StudyRepository by lazy { StudyRepository(mApiService) }
    private val mOfflineRepository: OfflineRepository by lazy { OfflineRepository(mApiService) }
    private val mUserRepository: UserRepository by lazy { UserRepository(mApiService) }

    private val _mUserProStudyTime = MutableLiveData<YcResult<UserProStudyTimeEntity>>()
    val mUserProStudyTime: LiveData<YcResult<UserProStudyTimeEntity>> = _mUserProStudyTime

    private val _mUserProJoinInfo = MutableLiveData<YcResult<UserProJoinInfoEntity>>()
    val mUserProJoinInfo: LiveData<YcResult<UserProJoinInfoEntity>> = _mUserProJoinInfo
    fun initData(proId: String) = ycLaunch {
        showLoading(delayTime = 0)
        mStudyRepository.getUserProStudyTime(proId).zip(mOfflineRepository.getJoinInfo(proId)) { data1, data2 ->
            _mUserProStudyTime.postValue(data1)
            _mUserProJoinInfo.postValue(data2)
        }.collect {
            hideLoading()
        }
    }


    private val _mLogout = MutableLiveData<YcResult<String?>>()
    val mLogout: LiveData<YcResult<String?>> = _mLogout
    fun logout() = ycLaunch {
        mUserRepository.logout().collect{ result ->
            _mLogout.postValue(result)
        }
    }
}